package graphic;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class ReleaseNotePanel extends JScrollPane {

	protected static ReleaseNotePanel RELEASENOTEPANEL;
	
	JTextPane note;
	
	StyledDocument logDocumentStyle;
	Style noteMessageStyle;
	
	public ReleaseNotePanel()
	{
		super();
		super.setLayout(new ScrollPaneLayout());
		
		this.note = new JTextPane();
		this.note.setEditable(false);
		this.note.setOpaque(false);
		this.note.setBorder(BorderFactory.createEmptyBorder(10, 10, 30, 10));
				
		super.getViewport().add(note);	
	}
	
	/**
	 * Writes a new messages into the chat area
	 * @param userId the player who sent the message
	 * @param msg the chat message
	 */
	public void setReleaseNote(String note)
	{   
		try {
			this.note.setStyledDocument(new DefaultStyledDocument());
			this.logDocumentStyle = this.note.getStyledDocument();
			this.initLogStyles();
			
			this.logDocumentStyle.insertString( 0, note, this.noteMessageStyle );
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initializes the styles of the chat area
	 */
	public void initLogStyles()
	{
		Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
		
	    this.noteMessageStyle = this.logDocumentStyle.addStyle( "chatMessage", def );
	    StyleConstants.setSuperscript( this.noteMessageStyle, true );
	    StyleConstants.setFontFamily(this.noteMessageStyle, "Comic Sans MS");
	    StyleConstants.setFontSize( this.noteMessageStyle, 15 );
	    StyleConstants.setForeground(this.noteMessageStyle, Color.black);
	    StyleConstants.setBold( this.noteMessageStyle, false );
	}
	
	public static ReleaseNotePanel getIstance()
	{
		if(RELEASENOTEPANEL == null) RELEASENOTEPANEL = new ReleaseNotePanel();
		
		return RELEASENOTEPANEL;
	}
	
}